import $ from 'jQuery';
import URI from 'URI'
require('../less/common.less');
require('../less/login.less');
$(function () {
    // 验证码
    var option = {
        mode: 0, // captcha
        captchaDom: '.form-verify-img',
        url: 'login/captcha'
    };
    $('.login-form-hook').IOTCaptcha(option);
    
    // 找回密码
    $('.pwd-forget-hook').off('click').on('click', function () {
        var url = URI.LOGIN.RETRIEVE_PASSWORD.PAGE;
        IOT.redirect2URL(url);
    });
    
    // 注册新用户
    /*$('.register-new-hook').off('click').on('click', function () {
        IOT.redirect2URL(URI.REGISTER.PAGE);
    });*/
    
    var $form = $('.login-form-hook');
    $form.validate({
        debug: true,
        rules: {
            login_username: {
                required: true,
                minlength: 1,
                maxlength: 30
            },
            login_password: {
                required: true,
                maxlength: 20
            },
            login_captcha: {
                required: true,
                maxlength: 10
            }
        },
        messages: {
            login_username: {
                required: '请输入手机号或邮箱'
            },
            login_password: {
                required:'请输入密码',
            },
            login_captcha: {
                required: '请输入验证码！'
            }
        },
        submitHandler: function (form) {
            IOT.showOverlay('登录中，请稍等...');
            var params = $form.serialize();
            var oldhash =params.split('&password=')[1].split('&')[0];
            var hash = encrypt(oldhash);
            params =params.replace(oldhash,hash);
            IOT.getServerData('/login', params, function (ret) {
                    IOT.hideOverlay();
                    if (ret.code === 200) {
                        IOT.tips('登录成功！', 'success', 1000, function () {
                            IOT.redirect2URL(ret.url);
                        });
                    } else {
                        console.log(JSON.stringify(ret));
                        IOT.tips(ret.msg || '登录失败！', 'error', 1000, function () {
                            if (ret.code === 401) {
                                $('#login_scode').trigger('click');
                            }
                        });
                    }
                },
                function () {
                    IOT.hideOverlay();
                    IOT.tips('系统忙请稍后再试！', 'error');
                }
            );
            return false;
        }
    });
    
    $(document).keyup(function (e) {
        if (e.keyCode === 13) {
            // $('.btn-login').trigger('click');
            // $form.submit();
        }
    });
    
    //aes 加密
    function encrypt(word){
        var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
        var srcs = CryptoJS.enc.Utf8.parse(word);
        var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
        return encrypted.toString();
    }
});

// 动态JS刷新
if (module.hot) {
    module.hot.accept();
}